<template>
  <div class="container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="分类名称" prop="category_name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="是否展示" prop="is_show" align="left">
        <el-radio-group v-model="ruleForm.isShow">
          <el-radio label="是">是</el-radio>
          <el-radio label="否">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        isShow: '',
      },
      rules: {
        name: [
          {required: true, message: '请输入分类名称', trigger: 'blur'},
          {min: 3, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        isShow: [
          {required: true, message: '请确定是否展示', trigger: 'blur'},
          // {pattern: /[a-zA-Z0-9_]{6,15}$/, message: '密码必须为6-15位的字母数字或下划线！', trigger: 'blur'}
        ]
      },
      action: '',
    };
  },
  created() {
    const {info, action} = this.$route.query
    if (action && action == 'edit') {
      this.action = 'edit'
      if(info) {
        this.ruleForm = JSON.parse(info)
      }
      // console.log(this.ruleForm)
    }else {
      this.action='add'
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm(`您确定保存该操作吗?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(async() => {
            // 确定操作
            if(this.action=='add'){
               const result=await this.$http.post("category/add",this.ruleForm)
              if(result.data.status=='success'){
                this.$message.success("添加成功,可以继续添加...")
                this.ruleForm={
                  name: '',
                  isShow: '',
                }
              }else{
                this.$message.error("添加失败,请稍后重试...")
                this.$router.push('admin')
              }
            }else if(this.action=='edit'){
              const result= await this.$http.put("category/edit",this.ruleForm)
              if(result.data.status=='success'){
                this.$message.success("修改成功")
              }else{
                this.$message.error("修改失败或没有修改,请稍后重试...")
              }
              this.$router.push('category')
            }
          }).catch(() => {
            // 取消操作
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  background-color: #fff;
  padding: 10px;

  .el-form {
    margin-top: 20px;
    width: 50%;
  }
}
</style>